package com.wanxi.controller;

import com.wanxi.model.CustomizeModel;
import com.wanxi.model.ResultModel;
import com.wanxi.service.CustomizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 杨金乾
 */
@Controller
@CrossOrigin(origins = "*")
public class CustomizeController{
    @Autowired
    private CustomizeService customizeService;

    @Autowired
    SendMessageController sendMessageController;
    private String table="定制";
    @RequestMapping("/back/customize/findAll")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel customizeFindAll(CustomizeModel model) {
//        //        1.乱码处理
////        req.setCharacterEncoding("UTF-8");
////        resp.setCharacterEncoding("UTF-8");
////        2.得到前端的值
////        page 第几页 limit 每一页显示的条数
//        String name = req.getParameter("name");
//        String content = req.getParameter("content");
//        String img = req.getParameter("img");
//        String id = req.getParameter("id");
//        String isShow = req.getParameter("isShow");
//
//
////        3.封装到model
//        CustomizeModel model = new CustomizeModel();
//        model.setImg(Tool.nullToStr(img));
//        model.setId(Tool.strToInt(id));
//        model.setName(Tool.nullToStr(name));
//        model.setContent(Tool.nullToStr(content));
//        model.setIsShow(Tool.strToInt(isShow));
//
//
////        4.调用service
//        CustomizeService customizeService = new CustomizeServiceImpl();
//        5.得到返回值
        ResultModel resultModel = customizeService.findAll(model);
        sendMessageController.findAllUserMessages(table);
//        6.返回给前端
        return resultModel;
    }

    @RequestMapping("/back/customize/findById")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel customizeFindById(CustomizeModel model) {

        ResultModel resultModel = customizeService.findById(model);
        sendMessageController.findByIdUserMessages(table);
//        6.返回给前端
        return resultModel;
    }

    @RequestMapping("/back/customize/isShow")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel customizeUpdateIsShow(CustomizeModel model) {

        ResultModel resultModel = customizeService.isShow(model);
        sendMessageController.updateIsShowMessages(table);
//        6.返回给前端
        return resultModel;
    }

    @RequestMapping("/back/customize/update")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel customizeUpdate(CustomizeModel model) {

        ResultModel resultModel = customizeService.update(model);
        sendMessageController.updateMessages(table);
//        6.返回给前端
        return resultModel;
    }
}
